<?php

    class Form_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
    }
    
    function index() {
        
        $section = 'form';
        
        $act = $this->input->get_post('act');
    
        $customer = new Customer();
        $year = '';
        $month = '';
        $day = '';
        
        if ($act == ACT_SUBMIT) {
            
            $customer->full_name = utf8_escape_textarea($this->input->post('full_name'));
            //$customer->email = utf8_escape_textarea($this->input->post('email'));
            $customer->career = utf8_escape_textarea($this->input->post('career'));
            $customer->gender = utf8_escape_textarea($this->input->post('gender'));
            $birth_date = $this->input->post('year').'-'.sprintf('%02d',$this->input->post('month')).'-'.sprintf('%02d',$this->input->post('day'));
            $customer->birth_date = $birth_date;
            $date = explode('-', $customer->birth_date);
            $year = $date[0];
            $month = $date[1];
            $day = $date[2];
            $customer->who_often_meet = utf8_escape_textarea($this->input->post('who_often_meet'));
            $customer->expect_inv_party = utf8_escape_textarea($this->input->post('expect_inv_party'));
            $customer->your_baileys = utf8_escape_textarea($this->input->post('your_baileys'));
            //$customer->picture = $this->input->post('your_baileys')
            
            if (!$customer->picture || $customer->picture == '') {
                $img_name = $this->do_upload('picture');
            }
            else {
                $img_name = $customer->picture;
            }
            
            if ($img_name) {
                
                $customer->picture = $img_name;
                
                if ($customer->validateInput()) {

                    if ($customer->insert()) {    
                        redirect("form/finish/$customer->id");
                    }
                    else {
                        $this->deleteFile($customer->picture);
                    }

                }
                else {
                    $this->deleteFile($customer->picture);
                }
                
            }
            else {
                MessageHandler::add(INVALID_PICTURE, MSG_ERROR, MESSAGE_ONLY);       
            }
            
        }
        
        $this->data['customer'] = $customer;
        $this->data['year'] = $year;
        $this->data['month'] = $month;
        $this->data['day'] = $day;
        $this->data['section'] = $section;
        $this->load->view('main', $this->data);
        
    }
    
    function do_upload($field)
    {
        $config['upload_path'] = config_item('upload_path');
        $config['allowed_types'] = config_item('allowed_types');
        $config['max_size']	= config_item('max_size');
        $config['max_width']  = config_item('max_width');
        $config['max_height']  = config_item('max_height');
        $config['encrypt_name'] = config_item('encrypt_name');

        $this->load->library('upload', $config);
            
        if ($this->upload->do_upload($field)) {
            
            $img = $this->upload->data();
            $config['image_library'] = config_item('image_library');
            $config['source_image'] = config_item('source_image').'/'.$img['file_name'];
            $config['create_thumb'] = config_item('create_thumb');
            $config['maintain_ratio'] = config_item('maintain_ratio');
            $config['width'] = config_item('width');
            $config['height'] = config_item('height');
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            return $img['file_name'];

        }
        else {
            
            if (!$this->upload->is_image()) {
                MessageHandler::add(INVALID_PICTURE, MSG_ERROR, MESSAGE_ONLY);
            }
            else {
                
                if (!$this->upload->is_allowed_dimensions()) {
                    MessageHandler::add(WRONG_FILE_DIMENSIONS, MSG_ERROR, MESSAGE_ONLY);
                }

                if (!$this->upload->is_allowed_filesize()) {
                    MessageHandler::add(WRONG_FILE_SIZE, MSG_ERROR, MESSAGE_ONLY);
                }

                if (!$this->upload->is_allowed_filetype()) {
                    MessageHandler::add(WRONG_FILE_TYPE, MSG_ERROR, MESSAGE_ONLY);
                }
                
            }
            
        }
        
        return false;
    }
    
    function finish($id) {
        
        $section = 'form';
    
        $customer = new Customer();
        $customer->get($id);
        
        $date = explode('-', $customer->birth_date);
        $year = $date[0];
        $month = $date[1];
        $day = $date[2];
        
        $this->data['customer'] = $customer;
        $this->data['day'] = $day;
        $this->data['month'] = $month;
        $this->data['year'] = $year;
        $this->data['section'] = $section;
        $this->data['no_submit'] = 1;
        $this->data['step'] = 'finish';
        $this->load->view('main', $this->data);
        
    }
    
    function thankyou($id) {
        
        $section = 'form';
    
        $customer = new Customer();
        $customer->get($id);
        
        $date = explode('-', $customer->birth_date);
        $year = $date[0];
        $month = $date[1];
        $day = $date[2];
        
        $this->data['customer'] = $customer;
        $this->data['day'] = $day;
        $this->data['month'] = $month;
        $this->data['year'] = $year;
        $this->data['section'] = $section;
        $this->data['no_submit'] = 1;
        $this->data['step'] = 'thankyou';
        $this->load->view('main', $this->data);
        
    }
    
    function deleteFile($image_file) {
        
        unlink(realpath(config_item('source_image')).'/'.$image_file);
        unlink(realpath(config_item('source_image')).'/'.str_replace('.jpg', '_thumb.jpg', strtolower($image_file)));
        
    }
    
}
